package com.example.smartall.ui;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.adapter.CarParkingListAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.CarParkingListBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
  * @Author:         abbas
  * @CreateDate:     2022/1/30
  * @Description:    停车模块首页
  */

public class CarParkingActivity extends BaseActivity {
    private ImageView imBack;
    private TextView tvTitle;
    private ImageButton ibList;
    private ListView lvNear;
    private Button btMore;

    private int pageNum = 1;
    private static int total = -1;
    private List<CarParkingListBean.RowsBean> parkingBeans = new ArrayList<>();
    private CarParkingListAdapter adapter;
    private MHandler handler;

    public CarParkingActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_parking);

        handler = new MHandler();

        init();
        initEvent();
        getParkingList(pageNum);
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ibList = (ImageButton) findViewById(R.id.ib_list);
        lvNear = (ListView) findViewById(R.id.lv_near);
        btMore = (Button) findViewById(R.id.bt_more);

        tvTitle.setText("停哪儿呢？");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CarParkingActivity.this.finish();
            }
        });
        ibList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 进入历史停车页面 此模块最主要的是好像也就这
                startActivity(new Intent(CarParkingActivity.this, CarParkingHistoryActivity.class));
            }
        });
        btMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (total < 0){
                    showToast("请等待数据加载成功...");
                    return;
                }
                if (total <= pageNum*6){
                    showToast("没有更多信息...");
                    return;
                }
                ++pageNum;
                getParkingList(pageNum);
            }
        });
        lvNear.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(CarParkingActivity.this, CarParkingDetailsActivity.class);
                intent.putExtra("id",parkingBeans.get(i).getId());
                startActivity(intent);
            }
        });
    }

    private void getParkingList(int pageNum){
        Api.config(ApiConfig.CAR_PARKING_LIST,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                CarParkingListBean bean = new Gson().fromJson(res,CarParkingListBean.class);
                if (bean != null && bean.getCode() == 200){
                    total = bean.getTotal();
                    parkingBeans.addAll(bean.getRows());
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {
            }
        },"?pageNum="+pageNum+"&pageSize=6");
    }

    private void fillListData(){
        Collections.sort(parkingBeans, new Comparator<CarParkingListBean.RowsBean>() {
            @Override
            public int compare(CarParkingListBean.RowsBean rowsBean, CarParkingListBean.RowsBean t1) {
                return Integer.parseInt(rowsBean.getDistance())-Integer.parseInt(t1.getDistance());
            }
        });
        if (adapter == null){
            adapter = new CarParkingListAdapter(CarParkingActivity.this,parkingBeans);
            lvNear.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    class MHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x01:
                    fillListData();
                    break;
            }
        }
    }
}